package com.py.custom.tms.application.atomic.str;

import java.util.Arrays;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * description：
 *
 * @author pengyou
 * @version 1.0.0
 * @date 2022/4/17
 */
public class StringTest {

    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(2);
        executorService.execute(()->{
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("-------------");
        });
        executorService.execute(()->{
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("-------------");
        });
        executorService.execute(()->{
            System.out.println("-------------");
        });

        char[] s = {'1', 'a', 'c'};
        reverseString(s);
        System.out.println("s = " + Arrays.toString(s));

        System.out.println("bacdfeg".equals(reverseStr("abcdefg", 2)));
        System.out.println(reverseWords("  hello world!  "));
    }

    /**
     * 反转数组
     *
     * @param s
     */
    public static void reverseString(char[] s) {
        int left = 0;
        int right = s.length - 1;

        while (left < right) {
            char temp = s[left];
            s[left] = s[right];
            s[right] = temp;
            left++;
            right--;
        }
    }

    /**
     * 给定一个字符串 s 和一个整数 k，你需要对从字符串开头算起的每隔 2k 个字符的前 k 个字符进行反转。
     * <p>
     * 如果剩余字符少于 k 个，则将剩余字符全部反转。
     * <p>
     * 如果剩余字符小于 2k 但大于或等于 k 个，则反转前 k 个字符，其余字符保持原样。
     *
     * @param s
     * @param k
     * @return
     */
    public static String reverseStr(String s, int k) {
        StringBuilder res = new StringBuilder();
        int start = 0;
        int length = s.length();
        while (start < length) {

            int firstK = start + k > length ? length : start + k;
            int secondK = start + 2 * k > length ? length : start + 2 * k;

            StringBuilder temp = new StringBuilder();
            temp.append(s, start, firstK);
            res.append(temp.reverse());

            if (firstK < secondK) {
                res.append(s, firstK, secondK);
            }

            start += 2 * k;
        }
        return res.toString();
    }

    /**
     * 给定一个字符串，逐个翻转字符串中的每个单词。
     *
     * @param s
     * @return
     */
    public static String reverseWords(String s) {

        int start = 0;
        int end = s.length() - 1;
        while (s.charAt(start) == ' ') {
            start++;
        }
        while (s.charAt(end) == ' ') {
            end--;
        }

        StringBuilder res = new StringBuilder();
        for (; start <= end; start++) {
            if (s.charAt(start) != ' ' || res.charAt(res.length() - 1) != ' ') {
                res.append(s.charAt(start));
            }
        }
        res.reverse();

        int startW = 0;
        int endW = 1;
        int n = res.length();
        while (startW < n) {
            while (endW < n && res.charAt(endW) != ' ') {
                endW++;
            }
            // 反转： startW, endW - 1;
            startW = endW + 1;
            endW = startW + 1;
        }
        return res.toString();
    }
}
